package com.kpx.app.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.kpx.app.R;
import com.kpx.app.base.ActivityBase;
import com.kpx.app.bean.WelcomeBean;
import com.kpx.app.cache.FileCache;
import com.kpx.app.configs.Constants;
import com.kpx.app.http.HttpDataApi;
import com.kpx.app.http.HttpDataLoader;
import com.kpx.app.http.HttpDataReqUrl;
import com.kpx.app.http.ResObj;
import com.kpx.app.utils.MyHashMap;
import com.kpx.app.utils.UniversualImageLoaderUtils;
import com.kpx.app.utils.uiutils.ToastUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by congdy on 17/4/10.
 */

public class ActivityWelcome extends ActivityBase implements ViewPager.OnPageChangeListener {
    @BindView(R.id.viewPager)
    ViewPager mViewPager;
    @BindView(R.id.welcome_bottom_image)
    ImageView welcomeBottomImage;
    @BindView(R.id.pagerIndicatorContainer)
    LinearLayout mPagerIndicatorContainer;
    @BindView(R.id.nextPageContainer)
    LinearLayout mNextPageContainer;
    private ImageView mSelectedIndicatorView;
    private GestureDetectorCompat mGestureDetector;
    private boolean mJumpOver;
    private int[] wpics = new int[]{R.drawable.w0, R.drawable.w1, R.drawable.w2, R.drawable.w3, R.drawable.w4};
    private int mCurrentSelectedPos;
    private List<String> imgs;
    private WelcomeBean btem;

    @Override
    public void initview(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_welcome);
        ButterKnife.bind(this);

    }

    @Override
    public void initData() {
        btem = (WelcomeBean) FileCache.sharedInstance().get(Constants.KEY_WELCOME_BEAN);
        if (btem != null) {
            imgs = btem.getImgs();
            mViewPager.setOffscreenPageLimit(imgs.size());
        }else {
            mViewPager.setOffscreenPageLimit(5);
        }
        mNextPageContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ActivityWelcome.this, ActivityMain.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                ActivityWelcome.this.startActivity(intent);
                ActivityWelcome.this.finish();
            }
        });
        mSelectedIndicatorView = (ImageView) mPagerIndicatorContainer.getChildAt(0);
        mViewPager.addOnPageChangeListener(this);

        mViewPager.setAdapter(new PagerAdapter() {

            @Override
            public int getCount() {
                if (btem != null) {
                    return imgs.size();
                }else {
                    return wpics.length;
                }
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                View pagerItemView = LayoutInflater.from(container.getContext()).inflate(R.layout.welcome_page, container, false);
                ImageView welcomePageView = (ImageView) pagerItemView.findViewById(R.id.welcomePageView);
                if (btem != null) {
                    Glide.with(ActivityWelcome.this).load(imgs.get(position)).into(welcomePageView);
                }else {
                    welcomePageView.setBackgroundResource(wpics[position]);
                }
                container.addView(pagerItemView);
                return pagerItemView;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }
        });
        mGestureDetector = new GestureDetectorCompat(ActivityWelcome.this, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {

                if (btem != null) {
                    if (mCurrentSelectedPos == imgs.size() - 1) {
                        if (mJumpOver == true) {
                            return true;
                        }
                        if (distanceX > 0) {
                            mJumpOver = true;
                        }
                    }
                }else {
                    if (mCurrentSelectedPos == wpics.length - 1) {
                        if (mJumpOver == true) {
                            return true;
                        }
                        if (distanceX > 0) {
                            mJumpOver = true;
                        }
                    }
                }
                return super.onScroll(e1, e2, distanceX, distanceY);
            }
        });
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        mGestureDetector.onTouchEvent(ev);
        if (mJumpOver) {
            Intent intent = new Intent(ActivityWelcome.this, ActivityMain.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            ActivityWelcome.this.startActivity(intent);
            ActivityWelcome.this.finish();
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public void onReceiver(Context context, Intent intent) {

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        mCurrentSelectedPos = position;
        mSelectedIndicatorView.setImageResource(R.drawable.unselected);
        ImageView selectedChild = (ImageView) mPagerIndicatorContainer.getChildAt(position);
        selectedChild.setImageResource(R.drawable.selected);
        mSelectedIndicatorView = selectedChild;
        if(btem!=null){
            if (position == imgs.size() - 1) {
                mNextPageContainer.setVisibility(View.VISIBLE);
            } else {
                mNextPageContainer.setVisibility(View.GONE);
            }
        }else {
            if (position == wpics.length - 1) {
                mNextPageContainer.setVisibility(View.VISIBLE);
            } else {
                mNextPageContainer.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
